<?php

namespace App\Admin\Controllers;

use App\Models\Deal;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DealController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '交易管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Deal);

        $grid->model()->where('is_deleted',0);
        $grid->column('id', __('Id'));
        $grid->user()->nickname(__('卖出人'));
        $grid->column('price', __('单价'));
        $grid->column('total_amount', __('总数量'));
        $grid->column('rest_amount', __('剩余数量'));
        $grid->column('created_at', __('发起时间'));
        $grid->column('status', __('状态'))->display(function($value){
            switch ($value) {
                case '1':
                    return "<span class='label label-warning'>进行中</span>";
                    break;
                case '2':
                    return "<span class='label label-danger'>已取消</span>";                    
                    break;
                case '3':
                    return "<span class='label label-success'>已完成</span>";                    
                    break;
                
                default:
                    # code...
                    break;
            }
        });
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Deal::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('price', __('Price'));
        $show->field('total_amount', __('Total amount'));
        $show->field('rest_amount', __('Rest amount'));
        $show->field('total_price', __('Total price'));
        $show->field('status', __('Status'));
        $show->field('is_deleted', __('Is deleted'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Deal);

        $form->number('user_id', __('User id'));
        $form->decimal('price', __('Price'));
        $form->number('total_amount', __('Total amount'));
        $form->number('rest_amount', __('Rest amount'));
        $form->decimal('total_price', __('Total price'))->default(0.00);
        $form->switch('status', __('Status'))->default(1);
        $form->switch('is_deleted', __('Is deleted'));

        return $form;
    }
}
